package com.hhl.lamda.streamsfilter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HHL
 * @description
 * @date 2018/11/17 - 14:18
 */
public class StreamsFilter {
    public static void main(String[] args) {

        /* 转为list集合 */
        List<String> lines = Arrays.asList("spring", "node", "HHL");

        List<String> result = getFilterOutput(lines, "HHL");
        result.forEach(System.out::print);
        System.out.println();



        System.out.println();
        System.out.println();
        System.out.println();
        /* Lamda 表达式过滤 */
        List<String> resultList= lines.stream()  // 将 list 转化为stream
                .filter(line -> !"HHL".equals(line))  // 过滤 HHL
                .collect(Collectors.toList());  // 收集除 HHL 之外的输出字段，并将stream转为list集合
        resultList.forEach(System.out::print); //output : spring, node
        System.out.println();

    }

    private static List<String> getFilterOutput(List<String> lines, String filter) {
        List<String> result = new ArrayList<>();
        for (String line : lines) {
            if (!"HHL".equals(line)) { // we dont like HHL
                result.add(line);
            }
        }
        return result;
    }
}
